/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CallTables.java
 *
 * Created on 31/08/2011, 23:12:13
 */

package ufpb.compiladores.lexico.run.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.table.DefaultTableModel;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Sarah
 */
public class CallTables extends javax.swing.JFrame {


    public CallTables (Document[] doc, File [] file) throws IOException{

        Object[] objectTabela = new Object[3];
        Object[] objectErros = new Object[2];

        
        this.setSize(1100,650);
        this.setLocationRelativeTo(null);
        
        Panel p = null;

        

        JTabbedPane jtp = new JTabbedPane();
        this.add(jtp);
        for(int i=0; i<doc.length; i++){ //chamar tables pra preencher, passar cada arquivo e lá preenche

            p = new Panel();
            p.addDocsAndFiles(doc, file);
            p.preencheTextArea(file[i]); //Colocar as linhas
            p.preencheNomeDoArquivo(file[i].getAbsolutePath());

            //jtp.addTab(file[i].getName(),p);
            
            jtp.add(file[i].getName(),p);
            p.setTabbedNumber(i);

            Node fc = doc[i].getFirstChild();

            if (fc.getNodeName().equals("Lexical")) {
                NodeList root2 = fc.getChildNodes();
                Node filho1 = root2.item(0);
                Node filho2 = root2.item(1);
                String filhoo = filho1.getNodeName();
                String filhoo2 = filho2.getNodeName();
                if(filhoo.equals("Tabela")){
                    NodeList listaElementos1 = filho1.getChildNodes();
                    int len = listaElementos1.getLength();
                    for(int m=0; m<len; m++){
                        Node aux = listaElementos1.item(m);
                        if(aux != null){
                            Node retorno = aux.getAttributes().getNamedItem("token");
                            objectTabela[0] = retorno.getTextContent();
                            retorno = aux.getAttributes().getNamedItem("descricao");
                            objectTabela[1] = retorno.getTextContent();
                            retorno = aux.getAttributes().getNamedItem("linha");
                            objectTabela[2] = retorno.getTextContent();
                            p.preencheLinhaTabela(objectTabela);
                        }
                    }

                }
                if(filhoo2.equals("Erros")){
                    NodeList listaErros = filho2.getChildNodes();
                    int len = listaErros.getLength();
                    for(int n=0; n<len; n++){
                        Node aux = listaErros.item(n);
                        if(aux!=null){
                            Node retorno = aux.getAttributes().getNamedItem("erro");
                            objectErros[0] = retorno.getTextContent();
                            retorno = aux.getAttributes().getNamedItem("linha");
                            objectErros[1] = retorno.getTextContent();
                            System.out.println(""+objectErros[1].toString());
                            p.preencheLinhaErro(objectErros);
                        }
                    }
                }
            }
       }
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jFrame2 = new javax.swing.JFrame();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
        jFrame2.getContentPane().setLayout(jFrame2Layout);
        jFrame2Layout.setHorizontalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame2Layout.setVerticalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1100, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 650, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFrame jFrame1;
    private javax.swing.JFrame jFrame2;
    // End of variables declaration//GEN-END:variables

}
